package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.common.db.DbContext;
import com.singfan.common.db.config.Table;
import com.singfan.core.dao.database.IShopHairAppointmentSummaryDao;
import com.singfan.core.dao.database.model.ShopHairAppointmentSummary;
import org.springframework.stereotype.Repository;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class ShopHairAppointmentSummaryDaoImpl extends DaoBaseGeneric<ShopHairAppointmentSummary> implements IShopHairAppointmentSummaryDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void create(ShopHairAppointmentSummary shopHairAppointmentSummary) {
        insertWithAutoIncrement(shopHairAppointmentSummary.getShopId(), shopHairAppointmentSummary);
    }

    @Override
    public void increaseAppointmentCount(long shopId, int appointmentDay, int appointmentClock) {
        String sqlTemp = "UPDATE " + getTableName(shopId) + " SET %1$s=%1$s+1 WHERE shop_id=? AND appointment_day=?";
        String appointmentClockName = "appointment_clock_";
        if (appointmentClock == 900){
            appointmentClockName += "0900";
        }
        else if(appointmentClock == 930){
            appointmentClockName += "0930";
        }
        else {
            appointmentClockName += appointmentClock;
        }
        String sql = String.format(sqlTemp, appointmentClockName);
        execute(shopId, sql, shopId, appointmentDay);
    }

    @Override
    public void reduceAppointmentCount(long shopId, int appointmentDay, int appointmentClock) {
        String sqlTemp = "UPDATE " + getTableName(shopId) + " SET %1$s=%1$s-1 WHERE shop_id=? AND appointment_day=?";
        String appointmentClockName = "appointment_clock_";
        if (appointmentClock == 900){
            appointmentClockName += "0900";
        }
        else if(appointmentClock == 930){
            appointmentClockName += "0930";
        }
        else {
            appointmentClockName += appointmentClock;
        }
        String sql = String.format(sqlTemp, appointmentClockName);
        execute(shopId, sql, shopId, appointmentDay);
    }

    @Override
    public int selectMaxAppointmentDay(long shopId) {
        int result = 0;
        String sql = "SELECT appointment_day FROM " + getTableName(shopId) + " WHERE shop_id=? ORDER BY appointment_day DESC limit 0,1";
        Object value = executeScalar(null, sql, shopId);
        if (value != null){
            result = Integer.parseInt(value.toString());
        }

        return result;
    }
}
